package com.admin.web.controller;

import com.bayss.core.DokoServerException;
import com.bayss.core.DokoServerExceptionFactor;
import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.delivery.DeliverymanInfo;
import com.bayss.core.service.deliveryman.DeliverymanService;
import com.bayss.core.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 骑手操作
 *
 * @author yangjian
 */
@Slf4j
@RestController
public class DeliverymanController extends ApiBaseController{

    private final DeliverymanService deliverymanService;

    @Autowired
    public DeliverymanController(DeliverymanService deliverymanService) {
        this.deliverymanService = deliverymanService;
    }

    /**
     * 骑手添加
     * @return
     */
    @RequestMapping("/deliveryman/save.json")
    public ResultMsg addDeliverymanInfo(@RequestBody DeliverymanInfo deliverymanInfo) {
        boolean res;

        String photoUrl = deliverymanInfo.getPhotoUrl();
        if (StringUtils.isNotBlank(photoUrl)) {
            deliverymanInfo.setPhotoUrl(FileUtil.formatImgUrl(photoUrl));
        }

        if (deliverymanInfo.getId() != null && deliverymanInfo.getId() != 0) {
            res = deliverymanService.update(deliverymanInfo);
        } else {
            res = deliverymanService.save(deliverymanInfo);
        }

        if (!res) {
            log.warn("add addr is fail");
            throw new DokoServerException(DokoServerExceptionFactor.DEFAULT, "save deliveryman is fail");
        }
        return ResultMsg.success();
    }

    /**
     * 查询骑手
     */
    @RequestMapping("/deliveryman/get.json")
    public ResultMsg updateDeliverymanInfo(@RequestParam(name = "stationId", required = false) Long stationId,
                                           @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                           @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ResultMsg.success(deliverymanService.query(stationId, page, pageSize));
    }


    /**
     * 删除骑手
     */
    @RequestMapping("/deliveryman/del.json")
    public ResultMsg delDeliverymanInfo(@RequestParam(name = "deliverymanId") Long deliverymanId) {
        boolean res = deliverymanService.del(deliverymanId);
        if (!res) {
            log.warn("del deliveryman is fail");
            throw new DokoServerException(DokoServerExceptionFactor.DEFAULT, "del deliveryman is fail");
        }
        return ResultMsg.success();
    }

    /**
     * 禁用骑手
     */
    @RequestMapping("/deliveryman/display.json")
    public ResultMsg display(@RequestParam(name = "deliverymanId") Long deliverymanId) {
        boolean res = deliverymanService.updateState(deliverymanId, 0);
        if (!res) {
            log.warn("display deliveryman is fail");
            throw new DokoServerException(DokoServerExceptionFactor.DEFAULT, "display deliveryman is fail");
        }
        return ResultMsg.success();
    }

    /**
     * 恢复骑手
     */
    @RequestMapping("/deliveryman/enable.json")
    public ResultMsg enable(@RequestParam(name = "deliverymanId") Long deliverymanId) {
        boolean res = deliverymanService.updateState(deliverymanId, 1);
        if (!res) {
            log.warn("enable deliveryman is fail");
            throw new DokoServerException(DokoServerExceptionFactor.DEFAULT, "enable deliveryman is fail");
        }
        return ResultMsg.success();
    }

    /**
     * 骑手考勤
     * @return
     */
    @RequestMapping("/deliveryman/login/info.json")
    public ResultMsg deliverymanLoginInfo(@RequestParam(name = "deliverymanId") Long deliverymanId,
                                          @RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                          @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ResultMsg.success(deliverymanService.queryLoginInfo(deliverymanId, page, pageSize));
    }


}
